package com.chenxingnet.controller;


import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.chenxingnet.dto.Cars;
import com.chenxingnet.dto.Department;
import com.chenxingnet.dto.vo.WaybillVo;
import com.chenxingnet.service.CarsService;
import com.chenxingnet.service.DepartmentService;
/**
 * 车辆管理
 * @author thinkpad
 *
 */
@Controller
@RequestMapping("/cars")
public class CarsController {
	
	@Resource
	private CarsService carsService;
	
	@Resource
	private DepartmentService departmentService;
	/**
	 * 转至新增车辆信息页面
	 * @return
	 */
	@RequestMapping("/insertCars")
	public String insertCars(Model model,HttpServletRequest request){
		try {
			Integer deptid = Integer.valueOf(request.getParameter("deptid"));
			if(deptid != null){
				model.addAttribute("deptid", deptid);
			}
			
		} catch (Exception e) {
			
		}
		
		List<Department> deps= departmentService.showDept();
		for (int i = 0; i < deps.size(); i++) {
			if(deps.get(i).getDeptname().equals("全部")){
				deps.remove(i);
			}
		}
		model.addAttribute("deps", deps);
		return "dengjibianji";
	}
	/**
	 * 转至修改信息页面
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping("/showCarsById")
	public String showCarsById(Model model,HttpServletRequest request){
		
		try {
			Integer deptid = Integer.valueOf(request.getParameter("deptid"));
			if(deptid != null){
				model.addAttribute("deptid", deptid);
			}
			
		} catch (Exception e) {
			
		}
		
		Integer cid = Integer.valueOf(request.getParameter("cid"));
		Cars cars = carsService.selectById(cid);
		model.addAttribute("cars", cars);
		return "dengjibianji";
		
	}
	/**
	 * 添加车辆信息
	 */
	@RequestMapping("/addCars")
	public String addCars(Cars cars,HttpServletRequest request,Model model){
		try {
			Integer deptid = Integer.valueOf(request.getParameter("deptid"));
			if(deptid != null){
				model.addAttribute("deptid", deptid);
			}
		} catch (Exception e) {
			
		}
		
		
		if(cars.getCid()==null){
			//id等于空，调用新增方法
			cars.setCpubdate(new Date());
			cars.setCreportstart("1");
			carsService.insertCars(cars);
		}else{
			//调用修改方法
			int i = carsService.updateCars(cars);
			System.out.println("修改"+i+"条车辆信息");
		}
		
		
		return "redirect:/cars/showCarsByCount?pageNow=1";
	}
	
	/**
	* @Title: /cars/mobile_addCars
	*移动端新增车辆信息
	 */
	@RequestMapping("/mobile_addCars")
	public void mobile_addCars(Cars cars,HttpServletRequest request,Model model,HttpServletResponse response){
		try {
			response.setHeader("Access-Control-Allow-Origin", "*");
			response.setContentType("application/json;charset=utf-8");
			JSONObject returnJson = new JSONObject();
			returnJson.put("falg","true");
			
			try {
				Integer deptid = Integer.valueOf(request.getParameter("deptid"));
				if(deptid != null){
					returnJson.put("deptid", deptid);
				}
			} catch (Exception e) {
				
			}
			Integer deptid = Integer.valueOf(request.getParameter("depid"));
			Department dept = departmentService.selectById(deptid);
			
			cars.setCdeptname(dept.getDeptname());
			cars.setCpubdate(new Date());
			cars.setCreportstart("1");
			int resu = carsService.insertCars(cars);
			
			returnJson.put("result",resu);//返回插入结果。
			response.getWriter().print(returnJson.toString());
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	//登记运单获取目的地      根据始发地  ajax
	@RequestMapping("/getDestinationByDeparture")
	@ResponseBody
	public Map<String,Object> determine(HttpServletRequest request,Model model,  HttpServletResponse httpServletResponse,String  thisusername){
		System.out.println("getDestinationByDeparture");
		httpServletResponse.setHeader("Access-Control-Allow-Origin", "*");
		httpServletResponse.setContentType("application/json;charset=utf-8");
		try {
			Integer deptid = Integer.valueOf(request.getParameter("deptid"));
			if(deptid != null){
				model.addAttribute("deptid", deptid);
			}
		} catch (Exception e) {
			
		}
		httpServletResponse.setCharacterEncoding("UTF-8");
		String chufadi = null;
		try {
			chufadi = new String(request.getParameter("object").getBytes("ISO-8859-1"),"UTF-8");
		} catch (UnsupportedEncodingException e) {
		}
		System.out.println(chufadi);
		List<Cars>  carsList=   carsService.getCarsByChufadi(chufadi);
		List<Cars> cars = removeBourn(carsList);
		
		
		Map<String, Object> result=new HashMap<String, Object>();
		if(cars.size()==0){
			result.put("result", "无相应目的地");
		}else{
			result.put("result", cars);
		}
		return  result;
	}
	

/**
* @Title: /car/mobile_getDestinationByDeparture 
*移动端运单状态请求
 */
@RequestMapping("/mobile_getDestinationByDeparture")
public void mobile_getDestinationByDeparture(HttpServletRequest request,Model model,HttpServletResponse response){
	try {
			response.setHeader("Access-Control-Allow-Origin", "*");
			response.setContentType("application/json;charset=utf-8");
			JSONObject returnJson = new JSONObject();
			returnJson.put("falg","true");
			try {
				Integer deptid = Integer.valueOf(request.getParameter("deptid"));
				if(deptid != null){
					returnJson.put("deptid", deptid);
				}
			} catch (Exception e) {
			}
/*			String   chufadi     =  request.getParameter("object");*/
			String chufadi = null;
			try {
				chufadi = new String(request.getParameter("object").getBytes("ISO-8859-1"),"UTF-8");
			} catch (UnsupportedEncodingException e) {
			}
			List<Cars>  carsList=   carsService.getCarsByChufadi(chufadi);
			List<Cars> cars = removeBourn(carsList);
			
			if(cars.size()==0){
				returnJson.put("result", "无相应目的地");
			}else{
				returnJson.put("result", cars);
			}
			response.getWriter().print(returnJson.toString());
	} catch (Exception e) {  e.printStackTrace();  }
}
		
		
	
	/**
	 * 目的地 去重
	 * @param cars
	 * @return
	 */
	List<Cars> removeBourn(List<Cars> cars){
		for (int i = 0; i < cars.size()-1; i++) {
			for (int j = cars.size()-1;j>i; j--) {
				if(cars.get(j).getCbourn().equals(cars.get(i).getCbourn())){
					cars.remove(j);
				}
			}
		}
		return cars;
	}
	
	
	
	//登记运单获取车型      根据始发地  ajax
	@RequestMapping("/getChexingnByDeparture")
	@ResponseBody
	public Map<String,Object> getChexingnByDeparture(HttpServletRequest request,Model model,  HttpServletResponse httpServletResponse,String  thisusername){
		try {
			Integer deptid = Integer.valueOf(request.getParameter("deptid"));
			if(deptid != null){
				model.addAttribute("deptid", deptid);
			}
		} catch (Exception e) {
			
		}
		httpServletResponse.setCharacterEncoding("UTF-8");
		String    mudi    =  request.getParameter("mudidiobject");
		String     chufadi   =  request.getParameter("shifaobject");
		/*String     chufadi = null;
		try {
			mudi    = new String(request.getParameter("mudidiobject").getBytes("ISO-8859-1"),"UTF-8");
			chufadi = new String(request.getParameter("shifaobject").getBytes("ISO-8859-1"),"UTF-8");
		} catch (UnsupportedEncodingException e) {
		}*/
		List<Cars>  carsList=   carsService.getChexingnByDeparture(chufadi,mudi);
		List<Cars> cars = removeMileage(carsList);
		
		Map<String, Object> result=new HashMap<String, Object>();
		if(cars.size()==0){
			result.put("result", "无相应目的地");
		}else{
			result.put("result", cars);
		}
		return  result;
	}
	
	/**
	* @Title: /car/mobile_getChexingnByDeparture 
	*移动端运单状态请求
	 */
	@RequestMapping("/mobile_getChexingnByDeparture")
	public void mobile_getChexingnByDeparture(HttpServletRequest request,Model model,HttpServletResponse response){
		try {
				response.setHeader("Access-Control-Allow-Origin", "*");
				response.setContentType("application/json;charset=utf-8");
				JSONObject returnJson = new JSONObject();
				returnJson.put("falg","true");
				try {
					Integer deptid = Integer.valueOf(request.getParameter("deptid"));
					if(deptid != null){
						returnJson.put("deptid", deptid);
					}
				} catch (Exception e) {
				}
				String    mudi    =  request.getParameter("mudidiobject");
				String     chufadi   =  request.getParameter("shifaobject");
				/*String    mudi    =  null;
				String     chufadi = null;
				try {
					mudi    = new String(request.getParameter("mudidiobject").getBytes("ISO-8859-1"),"UTF-8");
					chufadi = new String(request.getParameter("shifaobject").getBytes("ISO-8859-1"),"UTF-8");
				} catch (UnsupportedEncodingException e) {
				}*/
				List<Cars>  carsList=   carsService.getChexingnByDeparture(chufadi,mudi);
				List<Cars> cars = removeMileage(carsList);
				
				if(cars.size()==0){
					returnJson.put("result", "无相应目的地");
				}else{
					returnJson.put("result", cars);
				}
				response.getWriter().print(returnJson.toString());
		} catch (Exception e) {  e.printStackTrace();  }
	}

	
	
	
	
	/**
	 * 车型 去重
	 * @param cars
	 * @return
	 */
	List<Cars> removeMileage(List<Cars> cars){
		for (int i = 0; i < cars.size()-1; i++) {
			for (int j = cars.size()-1;j>i; j--) {
				if(cars.get(j).getCmileage().equals(cars.get(i).getCmileage())){
					cars.remove(j);
				}
			}
		}
		return cars;
	}
	
	
	//登记运单获取驱数     根据始发地  ajax
	@RequestMapping("/getQushuByDeparture")
	@ResponseBody
	public Map<String,Object> getQushuByDeparture(HttpServletRequest request,Model model,  HttpServletResponse httpServletResponse,String  thisusername){
		try {
			Integer deptid = Integer.valueOf(request.getParameter("deptid"));
			if(deptid != null){
				model.addAttribute("deptid", deptid);
			}
		} catch (Exception e) {
			
		}
		httpServletResponse.setCharacterEncoding("UTF-8");
		String    mudi    =  request.getParameter("mudidiobject");
		String     chufadi   =  request.getParameter("shifaobject");
		String     chexing   =  request.getParameter("chexingobject");
		/*String     chexing   =  null;
		String    mudi    =  null;
		String     chufadi = null;
		try {
			mudi    = new String(request.getParameter("mudidiobject").getBytes("ISO-8859-1"),"UTF-8");
			chufadi = new String(request.getParameter("shifaobject").getBytes("ISO-8859-1"),"UTF-8");
			chexing   =  new String(request.getParameter("chexingobject").getBytes("ISO-8859-1"),"UTF-8");
		} catch (UnsupportedEncodingException e) {
		}*/
		List<Cars>  carsList=   carsService.getQushuByDeparture(chufadi,mudi,chexing);
		List<Cars> cars = removeActuate(carsList);
		
		Map<String, Object> result=new HashMap<String, Object>();
		if(cars.size()==0){
			result.put("result", "无相应目的地");
		}else{
			result.put("result", cars);
		}
		return  result;
	}
	
	/**
	* @Title: /cars/mobile_getQushuByDeparture 
	*移动端运单状态请求
	 */
	@RequestMapping("/mobile_getQushuByDeparture")
	public void mobile_getQushuByDeparture(HttpServletRequest request,Model model,HttpServletResponse response){
		try {
				response.setHeader("Access-Control-Allow-Origin", "*");
				response.setContentType("application/json;charset=utf-8");
				JSONObject returnJson = new JSONObject();
				returnJson.put("falg","true");
				try {
					Integer deptid = Integer.valueOf(request.getParameter("deptid"));
					if(deptid != null){
						returnJson.put("deptid", deptid);
					}
				} catch (Exception e) {
				}
				String    mudi    =  request.getParameter("mudidiobject");
				String     chufadi   =  request.getParameter("shifaobject");
				String     chexing   =  request.getParameter("chexingobject");
				/*String     chexing   =  null;
				String    mudi    =  null;
				String     chufadi = null;
				try {
					mudi    = new String(request.getParameter("mudidiobject").getBytes("ISO-8859-1"),"UTF-8");
					chufadi = new String(request.getParameter("shifaobject").getBytes("ISO-8859-1"),"UTF-8");
					chexing   =  new String(request.getParameter("chexingobject").getBytes("ISO-8859-1"),"UTF-8");
				} catch (UnsupportedEncodingException e) {
				}*/
				List<Cars>  carsList=   carsService.getQushuByDeparture(chufadi,mudi,chexing);
				List<Cars> cars = removeActuate(carsList);
				
				if(cars.size()==0){
					returnJson.put("result", "无相应目的地");
				}else{
					returnJson.put("result", cars);
				}
				response.getWriter().print(returnJson.toString());
		} catch (Exception e) {  e.printStackTrace();  }
	}

	
	
	/**
	 * 驱数 去重
	 * @param cars
	 * @return
	 */
	List<Cars> removeActuate(List<Cars> cars){
		for (int i = 0; i < cars.size()-1; i++) {
			for (int j = cars.size()-1;j>i; j--) {
				if(cars.get(j).getCactuate().equals(cars.get(i).getCactuate())){
					cars.remove(j);
				}
			}
		}
		return cars;
	}
	
	
	//登记运单获取厂家结算    根据始发地  ajax
	@RequestMapping("/getChangjiaByDeparture")
	@ResponseBody
	public Map<String,Object> getChangjiaByDeparture(HttpServletRequest request,Model model,  HttpServletResponse httpServletResponse,String  thisusername){
		try {
			Integer deptid = Integer.valueOf(request.getParameter("deptid"));
			if(deptid != null){
				model.addAttribute("deptid", deptid);
			}
		} catch (Exception e) {
			
		}
		httpServletResponse.setCharacterEncoding("UTF-8");
		String    mudi    =  request.getParameter("mudidiobject");
		String     chufadi   =  request.getParameter("shifaobject");
		String     chexing   =  request.getParameter("chexingobject");
		String     qushu   =  request.getParameter("qushuobject");
		/*String     qushu   =  null;
		String     chexing   =  null;
		String    mudi    =  null;
		String     chufadi = null;
		try {
			mudi    = new String(request.getParameter("mudidiobject").getBytes("ISO-8859-1"),"UTF-8");
			chufadi = new String(request.getParameter("shifaobject").getBytes("ISO-8859-1"),"UTF-8");
			chexing   =  new String(request.getParameter("chexingobject").getBytes("ISO-8859-1"),"UTF-8");
			 qushu   =  new String(request.getParameter("qushuobject").getBytes("ISO-8859-1"),"UTF-8");
		} catch (UnsupportedEncodingException e) {
		}*/
		List<Cars>  cars=   carsService.getChangjiaByDeparture(chufadi,mudi,chexing,qushu);
		Map<String, Object> result=new HashMap<String, Object>();
		if(cars.size()==0){
			result.put("result", "无相应目的地");
		}else{
			result.put("result", cars);
		}
		return  result;
	}
	
	
	
	/**
	* @Title: /cars/mobile_getChangjiaByDeparture 
	*移动端运单状态请求
	 */
	@RequestMapping("/mobile_getChangjiaByDeparture")
	
	public void mobile_getChangjiaByDeparture(HttpServletRequest request,Model model,HttpServletResponse response){
		try {
				response.setHeader("Access-Control-Allow-Origin", "*");
				response.setContentType("application/json;charset=utf-8");
				JSONObject returnJson = new JSONObject();
				returnJson.put("falg","true");
				try {
					Integer deptid = Integer.valueOf(request.getParameter("deptid"));
					if(deptid != null){
						returnJson.put("deptid", deptid);
					}
				} catch (Exception e) {}
				String    mudi    =  request.getParameter("mudidiobject");
				String     chufadi   =  request.getParameter("shifaobject");
				String     chexing   =  request.getParameter("chexingobject");
				String     qushu   =  request.getParameter("qushuobject");
				/*String     qushu   =  null;
				String     chexing   =  null;
				String    mudi    =  null;
				String     chufadi = null;
				try {
					mudi    = new String(request.getParameter("mudidiobject").getBytes("ISO-8859-1"),"UTF-8");
					chufadi = new String(request.getParameter("shifaobject").getBytes("ISO-8859-1"),"UTF-8");
					chexing   =  new String(request.getParameter("chexingobject").getBytes("ISO-8859-1"),"UTF-8");
					 qushu   =  new String(request.getParameter("qushuobject").getBytes("ISO-8859-1"),"UTF-8");
				} catch (UnsupportedEncodingException e) {
				}*/
				List<Cars>  cars=   carsService.getChangjiaByDeparture(chufadi,mudi,chexing,qushu);
				if(cars.size()==0){
					returnJson.put("result", "无相应目的地");
				}else{
					returnJson.put("result", cars);
				}
				response.getWriter().print(returnJson.toString());
		} catch (Exception e) {  e.printStackTrace();  }
	}

	
	
	
	

	
	//登记运单获取驾驶员结算    根据始发地  ajax
	@RequestMapping("/getJiashiJiesuanByDeparture")
	@ResponseBody
	public Map<String,Object> getJiashiJiesuanByDeparture(HttpServletRequest request,Model model,  HttpServletResponse httpServletResponse,String  thisusername){
		try {
			Integer deptid = Integer.valueOf(request.getParameter("deptid"));
			if(deptid != null){
				model.addAttribute("deptid", deptid);
			}
		} catch (Exception e) {
			
		}
		httpServletResponse.setCharacterEncoding("UTF-8");
		String    mudi    =  request.getParameter("mudidiobject");
		String     chufadi   =  request.getParameter("shifaobject");
		String     chexing   =  request.getParameter("chexingobject");
		String     qushu   =  request.getParameter("qushuobject");
		String     changjiajiesuan   =  request.getParameter("changjiajiesuannobject");
		List<Cars>  cars=   carsService.getJiashiJiesuanByDeparture(chufadi,mudi,chexing,qushu,changjiajiesuan);
		Map<String, Object> result=new HashMap<String, Object>();
		if(cars.size()==0){
			result.put("result", "无相应目的地");
		}else{
			result.put("result", cars);
		}
		return  result;
	}
	
	
	/**
	* @Title: /cars/mobile_getJiashiJiesuanByDeparture 
	*移动端运单状态请求
	 */
	@RequestMapping("/mobile_getJiashiJiesuanByDeparture")
	public void mobile_getJiashiJiesuanByDeparture(HttpServletRequest request,Model model,HttpServletResponse response){
		try {
				response.setHeader("Access-Control-Allow-Origin", "*");
				response.setContentType("application/json;charset=utf-8");
				JSONObject returnJson = new JSONObject();
				returnJson.put("falg","true");
				try {
					Integer deptid = Integer.valueOf(request.getParameter("deptid"));
					if(deptid != null){
						returnJson.put("deptid", deptid);
					}
				} catch (Exception e) {}
				String    mudi    =  request.getParameter("mudidiobject");
				String     chufadi   =  request.getParameter("shifaobject");
				String     chexing   =  request.getParameter("chexingobject");
				String     qushu   =  request.getParameter("qushuobject");
				String     changjiajiesuan   =  request.getParameter("changjiajiesuannobject");
				List<Cars>  cars=   carsService.getJiashiJiesuanByDeparture(chufadi,mudi,chexing,qushu,changjiajiesuan);
				if(cars.size()==0){
					returnJson.put("result", "无相应目的地");
				}else{
					returnJson.put("result", cars);
				}
				response.getWriter().print(returnJson.toString());
		} catch (Exception e) {  e.printStackTrace();  }
	}

	
	
	
	
	
	
	/**
	 * 分页查询车辆信息
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/showCarsByCount")
	public String showCarsByCount(HttpServletRequest request,Model model){
		try {
			Integer deptid = Integer.valueOf(request.getParameter("deptid"));
			if(deptid != null){
				model.addAttribute("deptid", deptid);
			}
		} catch (Exception e) {
			
		}
		carsService.selectAllByCount(request, model);
		return "addCars";
		
		
	}
	
	/**
	* @Title: /cars/mobile_showCarsByCount
	*移动端分页查询车辆
	 */
	@RequestMapping("/mobile_showCarsByCount")
	public void mobile_showCarsByCount(HttpServletRequest request,Model model,HttpServletResponse response){
		
		
		try {
			
			response.setHeader("Access-Control-Allow-Origin", "*");
			response.setContentType("application/json;charset=utf-8");
			JSONObject returnJson = new JSONObject();
			returnJson.put("falg","true");
			try {
				Integer deptid = Integer.valueOf(request.getParameter("deptid"));
				if(deptid != null){
					returnJson.put("deptid", deptid);
				}
			} catch (Exception e) {
				
			}
			carsService.mobSelectAllByCount(request);
			
			returnJson.put("carList", request.getAttribute("carList"));
			returnJson.put("page", request.getAttribute("mobilePage"));
			
			response.getWriter().print(returnJson.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	
	/**
	 * 根据id删除车辆信息
	 */
	@RequestMapping(value="/deleteCars")
	public String deleteCars(HttpServletRequest request,Model model){
		try {
			Integer deptid = Integer.valueOf(request.getParameter("deptid"));
			if(deptid != null){
				model.addAttribute("deptid", deptid);
			}
		} catch (Exception e) {
			
		}
		
		Integer cid = Integer.valueOf(request.getParameter("cid"));
		carsService.deleteCars(cid);
		return "redirect:/cars/showCarsByCount?pageNow=1";
	}
	
	
}

